
package com.neegix.organization.post.interfaces.controller;

import com.neegix.base.PageVO;
import com.neegix.cqrs.command.UniversalCommandBus;
import com.neegix.cqrs.query.UniversalQueryBus;
import com.neegix.organization.post.application.service.command.DeletePostCommand;
import com.neegix.organization.post.application.service.command.UpdatePostCommand;
import com.neegix.organization.post.application.service.command.mapper.PostCommandMapper;
import com.neegix.organization.post.application.service.query.GetPagePostQuery;
import com.neegix.organization.post.application.service.query.GetPostDetailQuery;
import com.neegix.organization.post.application.service.query.mapper.PostQueryMapper;
import com.neegix.organization.post.interfaces.form.NewPostForm;
import com.neegix.organization.post.interfaces.form.QueryPostForm;
import com.neegix.organization.post.interfaces.form.UpdatePostForm;
import com.neegix.organization.post.interfaces.vo.PostVO;
import com.neegix.inferfaces.result.Result;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

 /**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-11-25 11:27:14
  */

@RestController
@RequestMapping("/api/v1/post")
public class PostController {

    @Autowired
    private UniversalCommandBus commandBus;

    @Autowired
    private UniversalQueryBus queryBus;

    @PreAuthorize("hasAuthority('organization:post:add')")
    @PostMapping
    public Result<Void> createPost(@RequestBody @Valid NewPostForm postForm){
        return Result.success("创建成功", commandBus.execute(PostCommandMapper.INSTANCE.covertCommand(postForm)));
    }

    @PreAuthorize("hasAuthority('organization:post:modify')")
    @PutMapping("/{id}")
    public Result<Void> updatePost(@PathVariable Long id, @RequestBody @Valid UpdatePostForm postForm){
        UpdatePostCommand command = PostCommandMapper.INSTANCE.covertCommand(postForm);
        command.setId(id);
        return Result.success("更新成功",commandBus.execute(command));
    }

    @PreAuthorize("hasAuthority('organization:post:list')")
    @GetMapping("/{currentPage}/{pageSize}")
    public Result<PageVO<PostVO>> getPosts(@PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize, @ModelAttribute QueryPostForm postForm){
        if(postForm == null) {
            postForm = new QueryPostForm();
        }

        GetPagePostQuery query = PostQueryMapper.INSTANCE.covertQuery(postForm);
        query.setCurrentPage(currentPage);
        query.setPageSize(pageSize);
        return Result.success("查询成功",queryBus.execute(query));
    }

    @PreAuthorize("hasAuthority('organization:post:get')")
    @GetMapping("/{id}")
    public Result<PostVO> getPostById(@PathVariable("id") Long id) {
        return Result.success("获取成功", queryBus.execute(new GetPostDetailQuery(id)));
    }

    @PreAuthorize("hasAuthority('organization:post:remove')")
    @DeleteMapping
    public Result<Void> removePost(@RequestBody List<Long> ids){
        return Result.success("删除成功", commandBus.execute(new DeletePostCommand(ids)));
    }
}